﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace Pahan.McuFusion.Common.Serialization
{
    /// <summary>
    /// The implementation of <see cref="IDeviceInfoListReader{TDeviceInfo}"/>
    /// for reading from JSON files.
    /// </summary>
    /// <typeparam name="TDeviceInfo">The type of device info.</typeparam>
    public class JsonDeviceInfoListReader<TDeviceInfo> : DeviceInfoListReader<TDeviceInfo>
        where TDeviceInfo : DeviceInfo
    {
        /// <summary>
        /// Performs actual reading from the list.
        /// </summary>
        protected override IEnumerable DoReadList(Stream input, Type type)
        {
            var listType = typeof(List<>).MakeGenericType(type);
            using (var reader = new StreamReader(input))
            {
                var serializer = new JsonSerializer
                                     {
                                         MissingMemberHandling = MissingMemberHandling.Ignore,
                                     };
                return (IEnumerable)serializer.Deserialize(reader, listType);
            }
        }
    }
}
